﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Input.Touch;
using System.IO.IsolatedStorage;
using System.IO;


namespace GameForKids
{

    public class CalculatorScreen : GameScreen
    {

        Button BTL;
        Button BTreset;
        Button BTmoNgoac;
        Button BTdongNgoac;

        Button BTT;
        Button BTsin;
        Button BTcos;
        Button BTtan;

        Button BTN;
        Button BTbinhPhuong;
        Button BTmuY;
        Button BTcan;

        Button BT0;
        Button BTphay;
        Button BT1;
        Button BT2;
        Button BT3;
        Button BT4;
        Button BT5;
        Button BT6;
        Button BT7;
        Button BT8;
        Button BT9;

        Button BTcong;
        Button BTtru;
        Button BTnhan;
        Button BTchia;

        Button BTxoa;
        Button BTclear;
        Button BTOK;

        SoundEffect soundclick;

        string dinhnghiaN ="";
        string dinhnghiaT = "";
        string dinhnghiaL = "";
        Vector2 posOfN;
        Vector2 posOfT;
        Vector2 posOfL;

        string TitleCongThucHienTai = "";
        string congThucHienTai = "";

        string TitleCongThucMoi = "";
        string CongThucMoi = "";

        Vector2 PosOfCongThucMoi;

        SpriteFont font;

        Boolean vechopchop = true;
        Boolean daphay = false;

        List<Button> ListButton;

        InputAction _quit_action;

        Texture2D background;
        Texture2D pixel;

        public CalculatorScreen()
        {

        }

        public override void Activate(bool instancePreserved)
        {
            _quit_action = new InputAction(new Buttons[] { Buttons.Back }, null, true);
            ListButton = new List<Button>();
            font = ScreenManager.Game.Content.Load<SpriteFont>("Fonts/HelpFont");
            soundclick = ScreenManager.Game.Content.Load<SoundEffect>("Login/Sounds/button_click");
            background = ScreenManager.Game.Content.Load<Texture2D>("Calculator/background");

            pixel = new Texture2D(ScreenManager.GraphicsDevice, 1,1);
            pixel.SetData(new[] { Color.White });

            string ctht = "";
            switch (GameForKids.gameState)
            {
                case GameState.Memory:
                    {
                        ctht = GamePlayMemory.CongThucHienTai;
                    }
                    break;
                case GameState.AlphabetMemory:
                    {
                        ctht = GamePlayMemory.CongThucHienTai;
                    }
                    break;
                case GameState.NumbersMemory:
                    {
                        ctht = GamePlayMemory.CongThucHienTai;
                    }
                    break;
                case GameState.SoundMemory:
                    {
                        ctht = GamePlayMemory.CongThucHienTai;
                    }
                    break;
                case GameState.Fishtank:
                    {
                        ctht = FishGamePlay.CongThucHienTai;
                    }
                    break;
                case GameState.FindCharacters:
                    {
                        ctht = FindCharactersGamePlay.CongThucHienTai;
                    }
                    break;
                case GameState.Puzzle:
                    {
                        ctht = GamePlayPuzzle.CongThucHienTai;
                    }
                    break;
                case GameState.FindSound:
                    {
                        ctht = FindSoundGamePlay.CongThucHienTai;
                    }
                    break;
                case GameState.Billiard:
                    {
                        ctht = BilliardGamePlay.CongThucHienTai;
                    }
                    break;
                case GameState.PackId:
                    {
                        ctht = GamePlayPacMan.CongThucHienTai;
                    }
                    break;
                default:
                    break;
            }
            CongThucMoi = ctht;
            PosOfCongThucMoi = new Vector2(GameForKids.WIDTH - 50 - font.MeasureString(CongThucMoi).X, 100);
            if (GameForKids.language == GameForKids.ANH)
            {
                dinhnghiaN = "N:  Moved in the level";
                dinhnghiaT = "T:  The time for pass level";
                dinhnghiaL = "L:  Current level";
                TitleCongThucHienTai = "Current formula";
                congThucHienTai = ctht;
                TitleCongThucMoi = "Scores =";
            }
            else
            {
                dinhnghiaT = "T:  Thời gian hoàn thành màn chơi";
                dinhnghiaN = "N:  Số bước thực hiện trong màn chơi";
                dinhnghiaL = "L:  Màn chơi hiện tại";
                TitleCongThucHienTai = "Công thức hiện tại";
                congThucHienTai = ctht;
                TitleCongThucMoi = "Điểm số =";
            }
            posOfN = new Vector2(800, 10);
            posOfT = new Vector2(850 + font.MeasureString(dinhnghiaN).X, 10);
            posOfL = new Vector2(900 + font.MeasureString(dinhnghiaT).X + font.MeasureString(dinhnghiaN).X, 10);

            BTL = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/btl"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btl_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btl_press"));
            BTL.Clicked += BTL_Clicked;

            BTreset = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/btreset"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btreset_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btreset_press"));
            BTreset.Clicked += BTreset_Clicked;

            BTmoNgoac = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/btngoacmo"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btngoacmo_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btngoacmo_press"));
            BTmoNgoac.Clicked += BTmoNgoac_Clicked;

            BTdongNgoac = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/btngoacdong"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btngoacdong_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btngoacdong_press"));
            BTdongNgoac.Clicked += BTdongNgoac_Clicked;

            BTT = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/btt"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btt_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btt_press"));
            BTT.Clicked += BTT_Clicked;

            BTsin = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/btsin"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btsin_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btsin_press"));
            BTsin.Clicked += BTsin_Clicked;

            BTcos = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/btcos"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btcos_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btcos_press"));
            BTcos.Clicked += BTcos_Clicked;

            BTtan = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/bttan"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/bttan_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/bttan_press"));
            BTtan.Clicked += BTtan_Clicked;

            BTN = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/btn"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btn_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btn_press"));
            BTN.Clicked += BTN_Clicked;

            BTbinhPhuong = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/btbinhphuong"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btbinhphuong_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btbinhphuong_press"));
            BTbinhPhuong.Clicked += BTbinhPhuong_Clicked;

            BTmuY = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/btmuy"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btmuy_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btmuy_press"));
            BTmuY.Clicked += BTmuY_Clicked;

            BTcan = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/btcan"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btcan_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btcan_press"));
            BTcan.Clicked += BTcan_Clicked;

            BT7 = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt7"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt7_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt7_press"));
            BT7.Clicked += BT7_Clicked;

            BT8 = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt8"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt8_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt8_press"));
            BT8.Clicked += BT8_Clicked;

            BT9 = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt9"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt9_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt9_press"));
            BT9.Clicked += BT9_Clicked;

            BTnhan = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/btnhan"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btnhan_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btnhan_press"));
            BTnhan.Clicked += BTnhan_Clicked;

            BTxoa = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/btxoa"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btxoa_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btxoa_press"));
            BTxoa.Clicked += BTxoa_Clicked;

            BT4 = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt4"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt4_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt4_press"));
            BT4.Clicked += BT4_Clicked;

            BT5 = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt5"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt5_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt5_press"));
            BT5.Clicked += BT5_Clicked;

            BT6 = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt6"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt6_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt6_press"));
            BT6.Clicked += BT6_Clicked;

            BTchia = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/btchia"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btchia_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btchia_press"));
            BTchia.Clicked += BTchia_Clicked;

            BTclear = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/btc"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btc_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btc_press"));
            BTclear.Clicked += BTclear_Clicked;

            BT1 = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt1"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt1_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt1_press"));
            BT1.Clicked += BT1_Clicked;

            BT2 = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt2"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt2_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt2_press"));
            BT2.Clicked += BT2_Clicked;

            BT3 = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt3"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt3_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt3_press"));
            BT3.Clicked += BT3_Clicked;

            BTcong = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/btcong"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btcong_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btcong_press"));
            BTcong.Clicked += BTcong_Clicked;

            BTOK = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/btok"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btok_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btok_press"));
            BTOK.Clicked += BTOK_Clicked;

            BT0 = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt0"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt0_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/bt0_press"));
            BT0.Clicked += BT0_Clicked;

            BTphay = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/btcham"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btcham_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/btcham_press"));
            BTphay.Clicked += BTphay_Clicked;

            BTtru = new Button(ScreenManager.Game.Content.Load<Texture2D>("Calculator/bttru"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/bttru_press"),
                ScreenManager.Game.Content.Load<Texture2D>("Calculator/bttru_press"));
            BTtru.Clicked += BTtru_Clicked;


            ListButton.Add(BTL);
            ListButton.Add(BTreset);
            ListButton.Add(BTmoNgoac);
            ListButton.Add(BTdongNgoac);
            ListButton.Add(BTT);
            ListButton.Add(BTsin);
            ListButton.Add(BTcos);
            ListButton.Add(BTtan);
            ListButton.Add(BTN);
            ListButton.Add(BTbinhPhuong);
            ListButton.Add(BTmuY);
            ListButton.Add(BTcan);
            ListButton.Add(BT7);
            ListButton.Add(BT8);
            ListButton.Add(BT9);
            ListButton.Add(BTnhan);
            ListButton.Add(BTxoa);
            ListButton.Add(BT4);
            ListButton.Add(BT5);
            ListButton.Add(BT6);
            ListButton.Add(BTchia);
            ListButton.Add(BTclear);
            ListButton.Add(BT1);
            ListButton.Add(BT2);
            ListButton.Add(BT3);
            ListButton.Add(BTcong);
            ListButton.Add(BTOK);
            ListButton.Add(BT0);
            ListButton.Add(BTphay);
            ListButton.Add(BTtru);


            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 4; j++)
                {

                     ListButton[i*4 +j].Location = new Point(20+80*j +5*j,225+70*i+5*i);
                }
            }


            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    if (i * 5 + j < 18)
                    {
                        ListButton[i * 5 + j + 12].Location = new Point(365 + 80 * j + 5 * j, 150 + 70 * i + 5 * i);
                        if(i * 5 + j >= 16)
                        ListButton[i * 5 + j + 12].Location = new Point(365 + 80 * (j+1) + 5 * (j+1), 150 + 70 * i + 5 * i);
                    }
                }
            }

            base.Activate(instancePreserved);
        }


        // button level
        void BTL_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            if (CongThucMoi.Length == 0)
            {
                CongThucMoi += "L";
            }
            else
            {
                string temp = CongThucMoi.Substring(CongThucMoi.Length - 1);
                if (temp == "(" || temp == ")" || temp == "+" || temp == "-" || temp == "*" || temp == "/" || temp == "^")
                {
                    CongThucMoi += "L";
                }
            }
        }
        

        // dau tru

        void BTtru_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();


            if (CongThucMoi.Length == 0)
            {
                CongThucMoi += "-";
                daphay = false;
            }
            else
            {
                int i = CongThucMoi.Length - 1;
                if ((CongThucMoi[i] >= '0' && CongThucMoi[i] <= '9')||
                    CongThucMoi[i]==')' || CongThucMoi[i] =='(' ||
                    CongThucMoi[i] == 'N' || CongThucMoi[i] == 'T' || CongThucMoi[i] == 'L')
                {
                    CongThucMoi += "-";
                    daphay = false;
                }
                else if (CongThucMoi[i] == '+' || CongThucMoi[i] == '*' || CongThucMoi[i] == '/')
                {
                    CongThucMoi = CongThucMoi.Substring(0, CongThucMoi.Length - 1) + "-";
                    daphay = false;
                }
            }    
        }


        // dau phay

        void BTphay_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            if (CongThucMoi.Length > 0)
            {
                int i = CongThucMoi.Length - 1;
                if (!daphay && CongThucMoi[i] <= '9' && CongThucMoi[i] >= '0')
                {
                    CongThucMoi += ".";
                    daphay = true;
                }
            }
        }


        // so khong

        void BT0_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

                CongThucMoi += "0";
        }



        void BTOK_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream isfs = isolatedStorageFile.CreateFile("CongThuc"+GameForKids.gameState.ToString()+".txt"))
                {
                    using (StreamWriter writer = new StreamWriter(isfs))
                    {
                        writer.WriteLine(CongThucMoi);
                    }
                }
            }
            switch (GameForKids.gameState)
            {
                case GameState.Memory:
                    {
                        GamePlayMemory.CongThucHienTai = CongThucMoi;
                    }
                    break;
                case GameState.AlphabetMemory:
                    {
                        GamePlayMemory.CongThucHienTai = CongThucMoi;
                    }
                    break;
                case GameState.NumbersMemory:
                    {
                        GamePlayMemory.CongThucHienTai = CongThucMoi;
                    }
                    break;
                case GameState.SoundMemory:
                    {
                        GamePlayMemory.CongThucHienTai = CongThucMoi;
                    }
                    break;
                case GameState.Fishtank:
                    {
                        FishGamePlay.CongThucHienTai = CongThucMoi;
                    }
                    break;
                case GameState.FindCharacters:
                    {
                        FindCharactersGamePlay.CongThucHienTai = CongThucMoi;
                    }
                    break;
                case GameState.Puzzle:
                    {
                        GamePlayPuzzle.CongThucHienTai = CongThucMoi;
                    }
                    break;
                case GameState.FindSound:
                    {
                        FindSoundGamePlay.CongThucHienTai = CongThucMoi;
                    }
                    break;
                case GameState.Billiard:
                    {
                        BilliardGamePlay.CongThucHienTai = CongThucMoi;
                    }
                    break;
                case GameState.PackId:
                    {
                        BilliardGamePlay.CongThucHienTai = CongThucMoi;
                    }
                    break;
                default:
                    break;
            }
            ExitScreen();
        }


        // dau cong
        void BTcong_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            if (CongThucMoi.Length == 0)
            {
                CongThucMoi += "+";
                daphay = false;
            }
            else
            {
                int i = CongThucMoi.Length - 1;
                if ((CongThucMoi[i] >= '0' && CongThucMoi[i] <= '9') ||
                    CongThucMoi[i] == ')' || CongThucMoi[i] == 'L' || 
                    CongThucMoi[i] == 'N' || CongThucMoi[i] == 'T')
                {
                    CongThucMoi += "+";
                    daphay = false;
                }
                else if (CongThucMoi[i] == '-' || CongThucMoi[i] == '*' || CongThucMoi[i] == '/')
                {
                    CongThucMoi = CongThucMoi.Substring(0, CongThucMoi.Length - 1) + "+";
                    daphay = false;
                }
            }
        }


        // so ba
        void BT3_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            CongThucMoi += "3";
        }


        // so hai
        void BT2_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            CongThucMoi += "2";
        }


        // so mot
        void BT1_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            CongThucMoi += "1";
        }


        // xoa het
        void BTclear_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            CongThucMoi = "";
            daphay = false;
        }


        // chia
        void BTchia_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            if (CongThucMoi.Length > 0)
            {
                int i = CongThucMoi.Length - 1;
                if ((CongThucMoi[i] >= '0' && CongThucMoi[i] <= '9') ||
                    CongThucMoi[i] == ')' || CongThucMoi[i] == 'L' || 
                    CongThucMoi[i] == 'N' || CongThucMoi[i] == 'T')
                {
                    CongThucMoi += "/";
                    daphay = false;
                }
                else if (CongThucMoi[i] == '+' || CongThucMoi[i] == '*' || CongThucMoi[i] == '-')
                {
                    CongThucMoi = CongThucMoi.Substring(0, CongThucMoi.Length - 1) + "/";
                    daphay = false;
                }
            }
        }


        // so sau
        void BT6_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            CongThucMoi += "6";
        }


        // so nam
        void BT5_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            CongThucMoi += "5";
        }


        // so bon
        void BT4_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            CongThucMoi += "4";
        }


        // xoa 1 ki tu
        void BTxoa_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            if (CongThucMoi.Length == 1)
                CongThucMoi = "";
            else if (CongThucMoi.Length > 1) 
            {
                string temp = CongThucMoi.Substring(CongThucMoi.Length-2);
                if (temp == "n(" || temp == "s(")
                {
                    CongThucMoi = CongThucMoi.Substring(0, CongThucMoi.Length - 4);
                }
                else if (temp == "t(")
                {
                    CongThucMoi = CongThucMoi.Substring(0, CongThucMoi.Length - 5);
                }
                else
                {
                    string p = CongThucMoi.Substring(CongThucMoi.Length-1);
                    if((p=="+" || p=="-" || p=="*" || p=="/")&& !daphay)
                     daphay = true;
                    CongThucMoi = CongThucMoi.Substring(0, CongThucMoi.Length - 1);
                }
            }
        }


        // nhan
        void BTnhan_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

             if (CongThucMoi.Length >0)
            {
                int i = CongThucMoi.Length - 1;
                if ((CongThucMoi[i] >= '0' && CongThucMoi[i] <= '9') ||
                    CongThucMoi[i] == ')' || CongThucMoi[i] == 'L' || 
                    CongThucMoi[i] == 'N' || CongThucMoi[i] == 'T')
                {
                    CongThucMoi += "*";
                    daphay = false;
                }
                else if (CongThucMoi[i] == '+' || CongThucMoi[i] == '-' || CongThucMoi[i] == '/')
                {
                    CongThucMoi = CongThucMoi.Substring(0, CongThucMoi.Length - 1) + "*";
                    daphay = false;
                }

            }
        }
        

        // so chin
        void BT9_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            CongThucMoi += "9";
        }


        // so tam
        void BT8_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            CongThucMoi += "8";
        }


        // so bay
        void BT7_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            CongThucMoi += "7";
        }


        // dau can
        void BTcan_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            if(CongThucMoi.Length == 0)
                CongThucMoi += "sqrt(";
            else if (CongThucMoi.Substring(CongThucMoi.Length - 1) != ".")
            {
                CongThucMoi += "sqrt(";
                daphay = false;
            }
        }


        // mu y
        void BTmuY_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            if (CongThucMoi.Length > 0)
            {
                int i = CongThucMoi.Length - 1;
                if(CongThucMoi[i] == ')' || (CongThucMoi[i] >='0' && CongThucMoi[i] <= '9'))
                CongThucMoi += "^";
                daphay = false;
            }
        }


        // mu 2
        void BTbinhPhuong_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            if (CongThucMoi.Length > 0)
            {
                int i = CongThucMoi.Length - 1;
                if (CongThucMoi[i] == ')' || (CongThucMoi[i] >= '0' && CongThucMoi[i] <= '9'))
                    CongThucMoi += "^2";
                daphay = false;
            }
        }


        // so n
        void BTN_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            if (CongThucMoi.Length == 0)
            {
                CongThucMoi += "N";
            }
            else
            {
                string temp = CongThucMoi.Substring(CongThucMoi.Length - 1);
                if (temp == "(" || temp == ")" || temp == "+" || temp == "-" || temp == "*" || temp == "/" || temp == "^")
                {
                    CongThucMoi += "N";
                }
            }
        }


        // tan
        void BTtan_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            if (CongThucMoi.Length == 0)
                CongThucMoi += "tan(";
            else if (CongThucMoi.Substring(CongThucMoi.Length - 1) != ".")
            {
                CongThucMoi += "tan(";
                daphay = false;
            }
        }


        // cos
        void BTcos_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            if (CongThucMoi.Length == 0)
                CongThucMoi += "cos(";
            else if (CongThucMoi.Substring(CongThucMoi.Length - 1) != ".")
            {
                CongThucMoi += "cos(";
                daphay = false;
            }
        }


        // sin
        void BTsin_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            if (CongThucMoi.Length == 0)
                CongThucMoi += "sin(";
            else if (CongThucMoi.Substring(CongThucMoi.Length - 1) != ".")
            {
                CongThucMoi += "sin(";
                daphay = false;
            }
        }


        // 
        void BTT_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            if (CongThucMoi.Length == 0)
            {
                CongThucMoi += "T";
            }
            else
            {
                string temp = CongThucMoi.Substring(CongThucMoi.Length - 1);
                if (temp == "(" || temp == ")" || temp == "+" || temp == "-" || temp == "*" || temp == "/" )
                {
                    CongThucMoi += "T";
                }
            }
        }


        // dong ngoac
        void BTdongNgoac_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            if (CongThucMoi.Length > 0)
            {
                string temp = CongThucMoi.Substring(CongThucMoi.Length-1);
                if(temp != "." || temp !="+"||
                    temp != "-" || temp !="*"||
                    temp != "/" || temp !="^" || temp!="(" || temp !=")")
                {
                    CongThucMoi+=")";
                    daphay = false;
                }
                
            }
        }


        // mo ngoac
        void BTmoNgoac_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            if (CongThucMoi.Length == 0)
                CongThucMoi += "(";
            else
            {
                string temp = CongThucMoi.Substring(CongThucMoi.Length - 1);
                if ( temp != "." )
                {
                    CongThucMoi += "(";
                    daphay = false;
                }
            }
        }


        // 
        void BTreset_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                soundclick.Play();

            switch (GameForKids.gameState)
            {
                case GameState.Memory:
                    {
                        CongThucMoi = GamePlayMemory.CongThucGoc;
                    }
                    break;
                case GameState.AlphabetMemory:
                    {
                        CongThucMoi = GamePlayMemory.CongThucGoc;
                    }
                    break;
                case GameState.NumbersMemory:
                    {
                        CongThucMoi = GamePlayMemory.CongThucGoc;
                    }
                    break;
                case GameState.SoundMemory:
                    {
                        CongThucMoi = GamePlayMemory.CongThucGoc;
                    }
                    break;
                case GameState.Fishtank:
                    {
                        CongThucMoi = FishGamePlay.CongThucGoc;
                    }
                    break;
                case GameState.FindCharacters:
                    {
                        CongThucMoi = FindCharactersGamePlay.CongThucGoc;
                    }
                    break;
                case GameState.Puzzle:
                    {
                        CongThucMoi = GamePlayPuzzle.CongThucGoc;
                    }
                    break;
                case GameState.FindSound:
                    {
                        CongThucMoi = FindSoundGamePlay.CongThucGoc;
                    }
                    break;
                case GameState.Billiard:
                    {
                        CongThucMoi = BilliardGamePlay.CongThucGoc;
                    }
                    break;
                case GameState.PackId:
                    {
                        CongThucMoi = GamePlayPacMan.CongThucGoc;
                    }
                    break;
                default:
                    break;
            }
        }

        public override void Unload()
        {
            ListButton = null;
        }




        public override void HandleInput(Microsoft.Xna.Framework.GameTime gameTime, InputState input)
        {


            PlayerIndex player;
            // thoat den dang nhap thi se thoat hoan toan
            if (_quit_action.Evaluate(input, ControllingPlayer, out player))
            {
                if (GameForKids.ISMUSIC)
                    soundclick.Play();
                ExitScreen();
            }

             TouchCollection touches = input.TouchState;
             if (touches.Count > 0)
             {
                 TouchLocation touch = touches[0];
                 {
                     foreach (Button b in ListButton)
                         b.TouchEvent(touch);
                 }
             }
            base.HandleInput(gameTime, input);
        }

        private float timetick=0;
        public override void Update(Microsoft.Xna.Framework.GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.Milliseconds;
            timetick += elapsed;
            if (timetick > 500)
            {
                timetick = 0;
                if (vechopchop)
                    vechopchop = false;
                else
                    vechopchop = true;
            }

            posOfN.X -= 4;
            posOfT.X -= 4;
            posOfL.X -= 4;

            if (posOfN.X < 0 - font.MeasureString(dinhnghiaN).X)
            {
                if (posOfT.X + font.MeasureString(dinhnghiaL).X + 50 > GameForKids.WIDTH)
                {
                    posOfN.X = posOfL.X + font.MeasureString(dinhnghiaL).X + 50;
                }
                else
                {
                    posOfN.X = GameForKids.WIDTH;
                }
            }

            if (posOfT.X < 0 - font.MeasureString(dinhnghiaT).X)
            {
                if (posOfN.X + font.MeasureString(dinhnghiaN).X + 50 > GameForKids.WIDTH)
                {
                    posOfT.X = posOfN.X + font.MeasureString(dinhnghiaN).X + 50;
                }
                else
                {
                    posOfT.X = GameForKids.WIDTH;
                }
            }

            if (posOfL.X < 0 - font.MeasureString(dinhnghiaL).X)
            {
                if (posOfT.X + font.MeasureString(dinhnghiaT).X + 50 > GameForKids.WIDTH)
                {
                    posOfL.X = posOfT.X + font.MeasureString(dinhnghiaT).X + 50;
                }
                else
                {
                    posOfL.X = GameForKids.WIDTH;
                }
            }


            PosOfCongThucMoi.X = GameForKids.WIDTH - 50 - font.MeasureString(CongThucMoi).X;

            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            SpriteBatch spritebatch = ScreenManager.SpriteBatch;
            spritebatch.Begin();

            spritebatch.Draw(pixel, new Rectangle(0, 0, GameForKids.WIDTH, GameForKids.HEIGHT), Color.White);

            spritebatch.DrawString(font,
                congThucHienTai,
                new Vector2(40, 185),
                Color.Green,
                0,
                Vector2.Zero,
                0.8f,
                SpriteEffects.None,
                0);
            spritebatch.DrawString(font, CongThucMoi, PosOfCongThucMoi, Color.Blue);

            spritebatch.Draw(background, new Rectangle(0, 0, GameForKids.WIDTH, GameForKids.HEIGHT), Color.White);

            spritebatch.DrawString(font, dinhnghiaN, posOfN, Color.White);
            spritebatch.DrawString(font, dinhnghiaT, posOfT, Color.White);
            spritebatch.DrawString(font, dinhnghiaL, posOfL, Color.White);
            spritebatch.DrawString(font,
                TitleCongThucHienTai,
                new Vector2(40, 158),
                Color.White,
                0,
                Vector2.Zero,
                0.8f,
                SpriteEffects.None,
                0);
            spritebatch.DrawString(font, TitleCongThucMoi, new Vector2(40, 100), Color.White);
          
            
            if (vechopchop)
                spritebatch.DrawString(font, "_",
                    PosOfCongThucMoi + new Vector2(font.MeasureString(CongThucMoi).X, 0),
                    Color.Blue);

            foreach (Button b in ListButton)
                b.Draw(spritebatch);

            spritebatch.End();
            base.Draw(gameTime);
        }
    }
}
